package com.thomas.jaspersoft.studio.example.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.HtmlExporter;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleHtmlExporterOutput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimplePdfExporterConfiguration;
import net.sf.jasperreports.export.SimplePdfReportConfiguration;
import net.sf.jasperreports.export.SimpleWriterExporterOutput;
import net.sf.jasperreports.export.SimpleXlsxReportConfiguration;

@Service
public class JasperExportService {
	
	private static final Log LOG = LogFactory.getLog(JasperExportService.class);
	
	/**
	 * 
	 * @param jasperPrint
	 * @param reportName the name with the absolutely path 
	 * @param reportType
	 * @throws JRException 
	 */
	public String export(JasperPrint jasperPrint, String reportName, String reportType) throws JRException{
		switch(reportType){
			case "pdf" : this.exportPDF(jasperPrint, reportName += ".pdf");
				break;
			case "xlsx" : this.exportExcel(jasperPrint, reportName += ".xlsx");
				break;
			case "csv" : this.exportCsv(jasperPrint, reportName += ".csv");
				break;
			case "html" : this.exportHtml(jasperPrint, reportName += ".html");
				break;
			default:  this.exportPDF(jasperPrint, reportName += ".pdf");
				break;
		}
		return reportName;
	}
	
	public void exportPDF(JasperPrint jasperPrint, String reportPath) throws JRException{
		LOG.debug("reportPath: " + reportPath);
		
		JRPdfExporter exporter = new JRPdfExporter();
		 
		exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
		exporter.setExporterOutput( new SimpleOutputStreamExporterOutput(reportPath));
		 
		SimplePdfReportConfiguration reportConfig  = new SimplePdfReportConfiguration();
		reportConfig.setSizePageToContent(true);
		reportConfig.setForceLineBreakPolicy(false);
		 
		SimplePdfExporterConfiguration exportConfig = new SimplePdfExporterConfiguration();
		exportConfig.setMetadataAuthor("Author");
		exportConfig.setEncrypted(true);
		exportConfig.setAllowedPermissionsHint("PRINTING");
		 
		exporter.setConfiguration(reportConfig);
		exporter.setConfiguration(exportConfig);
		 
		exporter.exportReport();
	}
	
	public void exportExcel(JasperPrint jasperPrint, String reportPath) throws JRException{
		this.exportExcel(jasperPrint, reportPath, null);
	}
	
	public void exportExcel(JasperPrint jasperPrint, String reportPath, String[] sheetNames) throws JRException{
		JRXlsxExporter exporter = new JRXlsxExporter();		  
		exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
		exporter.setExporterOutput( new SimpleOutputStreamExporterOutput(reportPath));
		SimpleXlsxReportConfiguration reportConfig = new SimpleXlsxReportConfiguration();
		reportConfig.setWhitePageBackground(false);
		reportConfig.setIgnoreCellBorder(true);
		reportConfig.setRemoveEmptySpaceBetweenRows(true);
		if(sheetNames != null){
			reportConfig.setSheetNames(sheetNames);	
		}
		exporter.setConfiguration(reportConfig);
		exporter.exportReport();
	}
	
	public void exportCsv(JasperPrint jasperPrint, String reportPath) throws JRException{
		JRCsvExporter exporter = new JRCsvExporter();
		  
		exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
		exporter.setExporterOutput(
		  new SimpleWriterExporterOutput(reportPath));
		 
		exporter.exportReport();
	}
	
	public void exportHtml(JasperPrint jasperPrint, String reportPath) throws JRException{
		HtmlExporter exporter = new HtmlExporter();  
		exporter.setExporterInput(new SimpleExporterInput(jasperPrint));

		exporter.setExporterOutput(
		  new SimpleHtmlExporterOutput(reportPath));
		 
		exporter.exportReport();
	}
}
